using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public enum AniType
{
    None = 0,
    Scale = 1,
    Offset = 2,
    Both = 3
}


public class UIClickHelper
{
    private static float aniTime = 0.1f;
    private static float aniScale = 1.1f;
    private static float aniOffset = -5f;
    private static string defaultSoundName = "click";

    public static void AddScale(GameObject go)
    {
        var uel = UIEventListener.Get(go);
        var tran = go.transform;
        Tweener tweenDown = null;
        uel.aniClickDown = () => { tweenDown = tran.DOScale(aniScale, aniTime).SetUpdate(true); };

        uel.aniClickUp = () =>
        {
            if (tweenDown != null)
            {
                tweenDown.Kill();
            }

            tran.DOScale(1, aniTime).SetUpdate(true);
        };
    }

    public static void AddOffset(GameObject go)
    {
        var uel = UIEventListener.Get(go);
        var tran = go.transform;
        Vector3 startPos = Vector3.zero;
        Vector3 endPos = Vector3.zero;
        Tweener tweenDown = null;

        uel.aniClickDown = () =>
        {
            if (startPos == Vector3.zero)
            {
                startPos = tran.localPosition;
                endPos = startPos + new Vector3(0, aniOffset, 0);
            }

            tweenDown = tran.DOLocalMove(endPos, aniTime).SetUpdate(true);
        };

        uel.aniClickUp = () =>
        {
            if (tweenDown != null)
            {
                tweenDown.Kill();
            }

            tran.DOLocalMove(startPos, aniTime).SetUpdate(true);
        };
    }

    public static void AddScaleAndOffset(GameObject go)
    {
        var uel = UIEventListener.Get(go);
        var tran = go.transform;
        Vector3 startPos = Vector3.zero;
        Vector3 endPos = Vector3.zero;
        Tweener tweenDown1 = null;
        Tweener tweenDown2 = null;
        uel.aniClickDown = () =>
        {
            if (startPos == Vector3.zero)
            {
                startPos = tran.localPosition;
                endPos = startPos + new Vector3(0, aniOffset, 0);
            }

            tweenDown1 = tran.DOScale(aniScale, aniTime).SetUpdate(true);
            tweenDown2 = tran.DOLocalMove(endPos, aniTime).SetUpdate(true);
        };

        uel.aniClickUp = () =>
        {
            tweenDown1?.Kill();
            tweenDown2?.Kill();
            tran.DOScale(1, aniTime).SetUpdate(true);
            tran.DOLocalMove(startPos, aniTime).SetUpdate(true);
        };
    }

    public static void AddSound(GameObject go, string soundName = null)
    {
        soundName ??= defaultSoundName;
        UIEventListener.Get(go).soundClick = () =>
        {
            var clip = Resources.Load<AudioClip>($"Sound/{soundName}");
            SoundManager.Instance.PlaySound(clip);
        };
    }

    public static void AddClick(GameObject go, Action<GameObject, PointerEventData> cb, AniType aniType = AniType.Both,
        string soundName = null,
        float waitTime = 0)
    {
        if (aniType == AniType.Scale)
        {
            AddScale(go);
        }
        else if (aniType == AniType.Offset)
        {
            AddOffset(go);
        }
        else if (aniType == AniType.Both)
        {
            AddScaleAndOffset(go);
        }

        AddSound(go, soundName);

        if (waitTime != 0)
        {
            UIEventListener.Get(go).SetWaitTime(waitTime);
        }

        UIEventListener.Get(go).onClick = cb;
    }

    public static void AddButtonClick(GameObject go, Action cb, string soundName = null)
    {
        soundName ??= defaultSoundName;
        var btn = go.GetComponent<Button>();
        if (btn != null)
        {
            btn.onClick.RemoveAllListeners();
            btn.onClick.AddListener(() =>
            {
                cb?.Invoke();
                var clip = Resources.Load<AudioClip>($"Sound/{soundName}");
                SoundManager.Instance.PlaySound(clip);
            });
        }
    }
}